
// 确保在页面加载后再执行代码
document.addEventListener('DOMContentLoaded', function () {
    const myHead = new Vue({
        el: '#myHead',
        data: {
            carDetailList: [],
            car: '',
            loginUser: {
                userId: "-1",
                loginName: '',
                type: '',
            },
        },
        methods: {
            // 获取车辆信息的函数
            getCar: function () {
                axios({
                    url: '/api/car/getCar',
                    params: {
                        loginName: this.loginUser.loginName
                    }
                }).then(rs => {
                    this.car = rs.data.car;
                    this.carDetailList = [];
                    for (let car in rs.data.carMap) {
                        this.carDetailList.push(rs.data.carMap[car]);
                    }
                }).catch(error => {
                    console.error("错误信息:", error);
                });
            },
        },
        created: function () {
            // 在Vue实例创建时检查用户是否已登录
            let token = window.sessionStorage.getItem('token');
            if (token === '' || token === null) {
                console.log("未登录");
            } else {
                // 用户已登录，从 Redis 获取用户信息
                axios({
                    url: '/api/user/findUserFromRedis'
                }).then(rs => {
                    if (rs.data.code === 200) {
                        this.loginUser.userId = rs.data.user.id;
                        this.loginUser.loginName = rs.data.user.loginName;
                        this.loginUser.type = rs.data.user.type;
                        this.getCar(); // 获取车辆信息
                    }
                }).catch(error => {
                    console.error("错误信息:", error);
                });
            }
        }
    });
});
